package com.slz3.business.frame.conf;

import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.task.DynamicScheduleTaskSecond;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.orderManagement.ordOrder.domain.OrdOrder;
import com.slz3.business.module.orderManagement.ordOrder.domain.OrderStatusEnum;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

import java.util.*;

import static com.slz3.business.frame.util.RedisClient.redisTemplate;

@Component
public class KeyExpiredListener implements MessageListener {

    @Override
    public void onMessage(Message message, byte[] pattern) {
        System.out.println("onPMessage pattern " + pattern + " " + " " + message);
        String channel = new String(message.getChannel());
        String str = (String) redisTemplate.getKeySerializer().deserialize(message.getBody());
        if (str.indexOf(RK.Service__ORDER__CLOSE_TIME_TMPL) > -1) {
            //订单未支付进行提醒
            String memberuuid = str.split(":Remind:")[1];//用户uuid
            String orderid = str.split(":Remind:")[0].toString().split("Order:")[2];
            List list = new ArrayList<UUID>();
            list.add(UUID.fromString(orderid));
            List<OrdOrder> allByUuids = Services.ordOrder.findAllByUuids(list);
            OrdOrder order = allByUuids.get(0);
            //获取用户
            list.clear();
            list.add(UUID.fromString(memberuuid));
            List allByUuids1 = Services.memMember.findAllByUuids(list);
            //判断此订单是否已支付 是否存在
            if(VE.isNotEmpty(order.getSerOrderStatus())&&(String.valueOf(OrderStatusEnum.JIAOYIGUANBI.ordinal()).equals(order.getSerOrderStatus())||OrderStatusEnum.DAIFUKUAN.ordinal()==order.getSerOrderStatus())){
                //进行短信提醒
                Map orderMap = Jedis.db0.getMap(RK.SERVICE__ORDER__CLOSE_TIME_REMIND__HASH.replace("${orderId}",order.getUuid().toString()).replace("$(uuid)",memberuuid));//订单内容
                orderMap.put("MESSAGETYPE","SMS_151991549");
                DynamicScheduleTaskSecond.sendMessage("订单未支付提醒",allByUuids1,orderMap);
                Jedis.db0.remove(RK.SERVICE__ORDER__CLOSE_TIME_REMIND__HASH.replace("${orderId}",order.getUuid().toString()).replace("$(uuid)",memberuuid));
            }
        } else if (str.indexOf(RK.DAW_WINNER_UNRECEIVE_REMIND__TMPL) > -1) {
            //抽奖活动结束前一天进行提醒
            String memberuuid = str.split(":Remind:")[1];//用户uuid
            String dawid = str.split(":Remind:")[0].split("Unreceive:")[1];
            Map orderMap = Jedis.db0.getMap(RK.DAW_WINNER_UNRECEIVE_REMIND__HASH.replace("${dawid}",dawid).replace("${uuid}",memberuuid));//抽奖活动内容
            /*Map $uuid = Jedis.db0.getMap(RK.MEM_MEMBER__INFO__$UUID$.replace("$uuid", memberuuid));*/
            //判断是否领取 领取后不进行提醒
            String content = "";
            ArrayList<UUID> list = new ArrayList<UUID>();
            list.clear();
            list.add(UUID.fromString(memberuuid));
            List allByUuids1 = Services.memMember.findAllByUuids(list);
            orderMap.put("MESSAGETYPE","SMS_151996674");
            orderMap.put("expireDate",new Date(new Date().getTime()+24*60*60*1000L));
            DynamicScheduleTaskSecond.sendMessage("奖品未领取提醒",allByUuids1,orderMap);
            Jedis.db0.remove(RK.DAW_WINNER_UNRECEIVE_REMIND__HASH);
        } else if(str.indexOf(RK.DAW_ACTIVITY_BEFORE_REMIND__TMPL) > -1){
            //活动开启前 多少分钟进行提醒
            String activityid = str.split("Remind:")[1];//活动uuid
            Map orderMap = Jedis.db0.getMap(RK.DAW_ACTIVITY_BEFORE_REMIND__HASH.replace("${uuid}",activityid));//抽奖活动内容
            orderMap.put("MESSAGETYPE","SMS_151996668");
            List allByUuids1 = Repositorys.memMember.findAll();
            DynamicScheduleTaskSecond.sendMessage("抽奖活动即将开始",allByUuids1,orderMap);
            Jedis.db0.remove(RK.DAW_ACTIVITY_BEFORE_REMIND__HASH);
        }
        System.out.println("message:" + message);
    }

}